// pages/person/vipplas/index.js
const app = getApp()
import Toast from "../../../vant/weapp/toast/toast";
import clickreply from "../../../utils/clickreply.js";
Page({
  data: {
    store: [],
    index: 0,
    cardList: [],
    listIndex: -1,
    is_user_show: true,
    is_user: 0,
    showUser: false,
    chargeprice: 0,
    isCardList: true
  },
  onLoad(options) {
    const that = this
    that.getCardList(options.store_id)
    // app.http.post(app.api.storecz, {}, (res) => {
    //   that.setData({
    //     store: res.data,
    //     store_id: options.store_id
    //   })


    //   if(options.store_id){
    //     for (let i in res.data) {        
    //       if (res.data[i].store_id == options.store_id) {
    //         that.setData({
    //           index: i,
    //         })
    //         return
    //       }
    //     }
    //   }

    // })
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    console.log(99, e)
    this.setData({
      showUser: true,
    });
    wx.setStorageSync("mobile", e.detail.mobile)
    wx.setStorageSync('userInfo', e.detail)
  },
  // 充值列表
  getCardList(store) {
    console.log(store)
    const that = this
    let store_id
    if (store) {

      store_id = store

    } else {
      store_id = that.data.store[that.data.index].store_id.toString()
    }

    app.http.post(app.api.addMoney, {
      store_id
    }, (res) => {
      if (res.data == "") {
        that.setData({
          cardList: res.data.vip,
          isCardList: true
        })
      } else {
        that.setData({
          cardList: res.data.vip,
          isCardList: false
        })
      }

    })

  },
  // 切换门店
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    })
    this.getCardList()
  },

  bindHideKeyboard: function (e) {

    this.setData({
      listIndex: -1,
      chargeprice: e.detail.value
    })
  },
  choosePrice(e) {
    if (e.currentTarget.dataset.id == this.data.listIndex) {
      this.setData({
        listIndex: -1,
      });
    } else {
      this.setData({
        chargeprice: 0,
        listIndex: e.currentTarget.dataset.id,
      });
    }

  },
  choosePrice0(e) {
    this.setData({
      chargeprice: 0,
      listIndex: -1,
    });

  },
  // 立即充值
  rechargeOrder() {
    if (clickreply.preventDuplicateClicks(5)) return;
    const that = this

    console.log(that.data.listIndex)
    let store_id = that.data.store_id;
    let chargeprice = that.data.chargeprice;
    let id = chargeprice ? '0' : that.data.cardList[that.data.listIndex].vip_id;
    app.http.post(app.api.rechargeOrder, {
      store_id,
      chargeprice,
      id
    }, (res) => {
      if (res.code !== 200) {
        wx.showLoading({
          title: res.message
        })
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
        return;
      }
      app.http.wecat(res.data.data.order_on, 1, (res) => {})

    })
  }
});